package com.question.business.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.question.business.user.entity.User;
import com.question.business.user.model.ao.UserDropDownAO;
import com.question.business.user.model.ao.UserPageAO;
import com.question.business.user.model.dto.UserCacheDTO;
import com.question.business.user.model.vo.UserDropDownVO;
import com.question.business.user.model.vo.UserVO;
import com.question.common.annotation.DataConvert;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 用户Mapper接口
 *
 * @author star
 * @since 2022-07-20
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 用户分页查询
     *
     * @param page 分页参数
     * @param ao   请求参数
     * @return Page<UserVO> 响应参数
     */
    @DataConvert
    Page<UserVO> queryUserPage(Page<UserVO> page, @Param("ao") UserPageAO ao);

    /**
     * 查询用户缓存
     *
     * @return Map<String, UserCacheDTO>
     */
    @MapKey("id")
    @Select("SELECT id, username, nick_name FROM t_user WHERE is_deleted = 0")
    Map<String, UserCacheDTO> queryUserCache();

    /**
     * 根据用户名称更新错误次数
     *
     * @param username 用户名称
     */
    @Update("UPDATE t_user SET error_number = error_number + 1, last_login_time = now() WHERE username = #{username} AND is_deleted = 0")
    void changeLoginErrorNumber(String username);

    /**
     * 根据用户id解锁
     *
     * @param id 用户ID
     */
    @Update("UPDATE t_user SET error_number = 0, last_login_time = null WHERE id = #{id} AND is_deleted = 0")
    void unlockUser(String id);

    /**
     * 用户下拉列表查询
     *
     * @param ao 请求参数
     * @return List<UserDropDownVO> 响应参数
     */
    List<UserDropDownVO> queryUserDropDown(UserDropDownAO ao);
}